package com.xuexiang.xuidemo.slice.status.adapter;

import com.xuexiang.xui_lib.ResourceTable;
import com.xuexiang.xui_lib.component.status.StatusLoader;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RoundProgressBar;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

public class DefaultStatusView extends DirectionalLayout implements Component.ClickedListener {

    private final ClickedListener mRetryListener;

    public DefaultStatusView(Context context, int status, ClickedListener retryListener) {
        super(context);
        setOrientation(VERTICAL);
        setAlignment(LayoutAlignment.CENTER);
        setLayoutConfig(new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT));
        int layoutId = getLayoutIdByStatus(status);
        if (layoutId != 0) {
            LayoutScatter.getInstance(context).parse(layoutId, this, true);
            if (layoutId == ResourceTable.Layout_xui_msv_layout_loading) {
                RoundProgressBar roundProgressBar = (RoundProgressBar) findComponentById(ResourceTable.Id_msv_progress);
                if (roundProgressBar != null) {
                    roundProgressBar.setIndeterminate(true);
                    roundProgressBar.setProgressColor(new Color(Color.getIntColor("#2A9CDC")));
                }
            }
        }
        mRetryListener = retryListener;
        setStatus(status);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(238, 243, 247));
        setBackground(shapeElement);
    }

    /**
     * 设置状态
     *
     * @param status 状态
     */
    private void setStatus(int status) {
        switch (status) {
            case StatusLoader.STATUS_LOAD_SUCCESS:
                setVisibility(Component.HIDE);
                break;
            case StatusLoader.STATUS_LOADING:
                setVisibility(Component.VISIBLE);
                break;
            case StatusLoader.STATUS_LOAD_FAILED:
            case StatusLoader.STATUS_EMPTY_DATA:
            case StatusLoader.STATUS_CUSTOM:
                setVisibility(Component.VISIBLE);
                setClickedListener(this);
                break;
            default:
                break;
        }
    }

    private int getLayoutIdByStatus(int status) {
        int layoutId = 0;
        switch (status) {
            case StatusLoader.STATUS_LOADING:
                layoutId = ResourceTable.Layout_xui_msv_layout_loading;
                break;
            case StatusLoader.STATUS_LOAD_FAILED:
                layoutId = ResourceTable.Layout_xui_msv_layout_error;
                break;
            case StatusLoader.STATUS_EMPTY_DATA:
                layoutId = ResourceTable.Layout_xui_msv_layout_empty;
                break;
            case StatusLoader.STATUS_CUSTOM:
                layoutId = ResourceTable.Layout_xui_msv_layout_no_network;
                break;
            default:
                break;
        }
        return layoutId;
    }

    @Override
    public void onClick(Component component) {
        if (mRetryListener != null) {
            mRetryListener.onClick(component);
        }
    }
}
